package com.qiuzhiyi.dialogs;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.li.utils.ScreenUtil;
import com.qiuzhiyi.R;

/**
 * Author: Li
 * Date: 2017/11/14 0014
 * Description:
 */

public class ChooseDialog implements View.OnClickListener {

    private Context mContext;
    private AlertDialog mDialog;
    private String mContent;
    private int mIndex;
    private OnChooseListener mOnChooseListener;
    private String mTitle;
    private String[] mChooseList;

    public ChooseDialog(Context context, OnChooseListener onChooseListener, String title, String[] chooseList) {
        mContext = context;
        mOnChooseListener = onChooseListener;
        mTitle = title;
        mChooseList = chooseList;
        initDialog();
    }

    private void initDialog() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_choose, null);

        TextView txtTitle = view.findViewById(R.id.txt_title);
        TextView txtContent1 = view.findViewById(R.id.txt_content1);
        TextView txtContent2 = view.findViewById(R.id.txt_content2);

        txtTitle.setText(mTitle);
        txtContent1.setText(mChooseList[0]);
        txtContent2.setText(mChooseList[1]);

        txtContent1.setOnClickListener(this);
        txtContent2.setOnClickListener(this);

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
                .setView(view);

        mDialog = builder.create();
    }

    public void show() {
        mDialog.show();
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(
                    ContextCompat.getColor(mContext, R.color.transparent)));
            window.getDecorView().setPadding((int) (ScreenUtil.getScreenWidth(mContext) * 0.1f),
                    0, (int) (ScreenUtil.getScreenWidth(mContext) * 0.1f), 0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_content1:
                mIndex = 0;
                break;
            case R.id.txt_content2:
                mIndex = 1;
                break;
        }
        mContent = ((TextView) v).getText().toString();
        mOnChooseListener.onChoose(mContent,mIndex);
        mDialog.dismiss();
    }

    public interface OnChooseListener {
        void onChoose(String content, int which);
    }
}
